package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.dao.impl.NavImpl;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.NavService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavServiceImpl implements NavService {
    @Override
    public List<NavModel> getNavList() {
        List<NavModel> list = new ArrayList<>();
        Jedis jedis = new Jedis();
        Boolean exists = jedis.exists("titles");
        if (exists) {
            List<String> titles = jedis.lrange("titles", 0, jedis.llen("titles"));
            List<String> hrefs = jedis.lrange("hrefs", 0, -1);
            for (int i = 0; i < titles.size(); i++) {
                NavModel model = new NavModel();
                model.setTitle(titles.get(i));
                model.setHref(hrefs.get(i));
                list.add(model);
            }
        } else {
            NavDao navDao = new NavImpl();
            list = navDao.getNavList();
//            赋值
            for (NavModel model : list
            ) {
                jedis.rpush("titles", model.getTitle());
                jedis.rpush("hrefs", model.getHref());
            }

        }
        return list;
    }

    @Override
    public ResultModel findAll(NavModel navModel) {
        return null;
    }

    @Override
    public ResultModel add(NavModel navModel) {
        return null;
    }

    @Override
    public ResultModel findById(NavModel navModel) {
        return null;
    }

    @Override
    public ResultModel update(NavModel navModel) {
        NavDao navDao=new NavImpl();
        Jedis jedis=new Jedis();
        int update = navDao.update(navModel);
        if(update>0){
            jedis.del("titles","hrefs");
        }
        return null;
    }

    @Override
    public ResultModel delete(NavModel navModel) {
        return null;
    }

    @Override
    public ResultModel getCount(NavModel navModel) {
        return null;
    }
}
